 /*
 * @description: 活动详情
 * @Author: along
 * @Date: 2019-08-31
 * @Last Modified by: liyujie
 * @Last Modified time: 2020-12-30
 */
<template>
    <div class="container politeActiveDetail">
        <!-- header -->
        <div class="header-nav">
            <div class="header-left">
                <div class="header-new-box">
                    <p class="header-new-box-title">用户昵称或电话:</p>
                    <el-input
                        v-model="formData.keyword"
                        placeholder="用户昵称或电话"
                        size="small"
                        clearable
                        suffix-icon="el-icon-search"
                        @change="getTableData(1)"
                        @keyup.native.enter="getTableData(1)"
                    />
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">白名单用户:</p>
                    <el-select
                        v-model="formData.in_white_list"
                        placeholder="全部"
                        size="small"
                        @change="getTableData(1)"
                    >
                        <el-option
                            label="全部"
                            :value="99"
                        />
                        <el-option
                            label="是"
                            :value="1"
                        />
                        <el-option
                            label="否"
                            :value="0"
                        />
                    </el-select>
                </div>
                <div class="header-new-box">
                    <p class="header-new-box-title">邀请时间:</p>
                    <el-date-picker
                        v-model="dateTimeRange"
                        type="datetimerange"
                        range-separator="-"
                        start-placeholder="开始日期"
                        end-placeholder="结束日期"
                        :default-time="['00:00:00', '23:59:59']"
                        size="small"
                        style="width: 400px"
                        value-format="timestamp"
                    />
                </div>
            </div>
        </div>
        <!-- /header -->

        <!-- table -->
        <el-table
            :data="tableData"
            height="100%"
            style="width: 100%;"
            border
            :resizable="false"
            ref="politeActiveDetail"
            @sort-change="handleSortChanged"
        >
            <el-table-column
                type="index"
                width="80"
                label="序号"
                align="center"
            />
            <el-table-column
                prop="true_name"
                label="邀请者"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="phone"
                label="邀请者电话"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
            />
            <el-table-column
                prop="agent_level"
                label="邀请者类型"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            >
                <template slot-scope="scope">
                    <p v-if="scope.row.agent_level === -1">普通用户</p>
                    <p v-if="scope.row.agent_level !== -1">E{{ scope.row.agent_level }}</p>
                </template>
            </el-table-column>
            <el-table-column
                prop="register_student_count"
                label="累计邀请注册用户"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            />
            <el-table-column
                prop="pay_order_student_count"
                label="邀请下单人数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            />
            <el-table-column
                prop="pay_order_complete_student_count"
                label="实付达标人数"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
                align="center"
            />
            <el-table-column
                prop="invite_complete_student_count"
                label="实际邀请成功用户"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                sortable="custom"
                align="center"
            />
            <el-table-column
                prop="cannot_withdraw_award_money"
                label="预计收益"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            />
            <el-table-column
                prop="award_money"
                label="已入账"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            />
            <el-table-column
                prop="award_money_total"
                label="累计奖励"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            />
            <el-table-column
                prop="pay_order_complete_award_money"
                label="完成下单用户的累计奖励"
                show-overflow-tooltip
                :formatter="emptyFormatter"
                align="center"
                sortable="custom"
            />
            <el-table-column
                label="操作"
                width="170"
            >
                <template slot-scope="scope">
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="handleChackDetail(scope.row)"
                    >
                        查看详情
                    </el-button>
                    <el-button
                        type="text"
                        class="curse-add"
                        @click="handleWhiteList(scope.row)"
                    >
                        {{ scope.row.in_white_list ? '移出白名单' : '加入白名单' }}
                    </el-button>
                </template>
            </el-table-column>
        </el-table>
        <!-- /table -->

        <!-- /分页 -->
        <div class="pagination-wrapper clearfix">
            <el-pagination
                background
                layout="total, prev, pager, next"
                :current-page.sync="page.now_page"
                :total="page.total_count"
                :page-size="page.page_size"
                @current-change="getTableData"
            />
        </div>

        <!-- 查看详情 -->
        <el-dialog
            title="查看详情"
            width="818px"
            :visible.sync="dialog"
            class="dialogPolite"
        >
            <div
                class="header-nav"
                :style="{justifyContent: 'flex-end'}"
            >
                <div class="header-left">
                    <div class="header-new-box">
                        <el-input
                            v-model="keyword_student_id"
                            placeholder="请输入姓名或ID"
                            size="small"
                            clearable
                            suffix-icon="el-icon-search"
                            @change="getDialogData(1)"
                            @keyup.native.enter="getDialogData(1)"
                        />
                    </div>
                    <div class="header-new-box">
                        <p class="header-new-box-title">注册时间:</p>
                        <el-date-picker
                            v-model="dialogTimeRange"
                            type="datetimerange"
                            range-separator="-"
                            start-placeholder="开始日期"
                            end-placeholder="结束日期"
                            :default-time="['00:00:00', '23:59:59']"
                            size="small"
                            style="width: 300px"
                            value-format="timestamp"
                        />
                    </div>
                </div>
            </div>
            <el-table
                :data="dialogData"
                height="100%"
                style="width: 100%;max-height: 600px"
                border
            >
                <el-table-column
                    prop="true_name"
                    label="被邀请者姓名"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
                <el-table-column
                    prop="student_no"
                    label="ID"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
                <el-table-column
                    prop="phone"
                    label="联系方式"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
                <el-table-column
                    prop="create_time"
                    label="注册时间"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                >
                    <template slot-scope="scope">
                        {{ scope.row.create_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="first_pay_order_time"
                    label="首单时间"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                >
                    <template slot-scope="scope">
                        {{ scope.row.first_pay_order_time * 1000 | dateFormatter('yyyy/MM/dd HH:mm') }}
                    </template>
                </el-table-column>
                <el-table-column
                    prop="status"
                    label="任务状态"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                >
                    <template slot-scope="scope">
                        <div>{{ scope.row.status | statusFilter }}</div>
                    </template>
                </el-table-column>
                <el-table-column
                    prop="school"
                    label="学校名称"
                    show-overflow-tooltip
                    :formatter="emptyFormatter"
                    align="center"
                />
            </el-table>
            <div
                class="pagination-wrapper clearfix"
                :style="{marginTop: '20px'}"
            >
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page.sync="dialog_page.now_page"
                    :total="dialog_page.total_count"
                    :page-size="dialog_page.page_size"
                    @current-change="getDialogData"
                />
            </div>
            <div :style="{display: 'flex', justifyContent: 'center', borderTop: '1px #eee solid'}">
                <el-button
                    size="small"
                    type="primary"
                    :style="{width: '120px', height: '32px',marginTop: '20px'}"
                    @click="dialog = false"
                >
                    我知道了
                </el-button>
            </div>
        </el-dialog>

        <!-- 图片预览 -->
        <img-preview ref="preview" />
    </div>
</template>

<script>
import imgPreview from '@/components/common/img-preview';
import { back } from '@/mixin/back';
export default {
    mixins: [back],
    components: {
        imgPreview
    },
    data () {
        return {
            student_no: '',
            dialogData: [],
            keyword_student_id: '',
            dialog: false,
            formData: {
                keyword: '',
                in_white_list: 99,
                invite_register_start_time: '',
                invite_register_end_time: ''
            },
            // 邀请时间
            dateTimeRange: [],
            // 被邀请注册时间
            dialogTimeRange: [],
            filterData: {
                sort_field: 0,
                sort_type: 1
            },
            page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            dialog_page: {
                now_page: 1,
                total_count: 0,
                page_size: 0
            },
            tableData: [],
            // 被邀请人注册时间
            create_start_time: '',
            create_end_time: '',
            authResult: {}
        };
    },
    filters: {
        statusFilter(val) {
            switch (val) {
                case 1:
                    return '待下单';
                case 2:
                    return '待入账';
                case 3:
                    return '已入账(完成)';
                case -1:
                    return '失效';
                default:
                    return '--';
            }
        }
    },
    computed: {},
    watch: {
        dialog (val) {
            if(!val) {
                this.keyword_student_id = '';
            }
        },
        dateTimeRange(value) {
            this.formData.invite_register_start_time = value ? value[0] / 1000 : '';
            this.formData.invite_register_end_time = value ? value[1] / 1000 : '';
            this.getTableData(1);
        },
        dialogTimeRange(value) {
            this.create_start_time = value ? value[0] / 1000 : '';
            this.create_end_time = value ? value[1] / 1000 : '';
            this.getDialogData(1);
        },
    },
    mounted () {
        this.$store.commit('SET_HEADER', [{ title: "APP后台"},{title: '商城'},{title: '邀请有礼'},{title: '活动详情'}]);
        this.authResult = this.$route.meta.authResult;
        this.getTableData();
    },
    methods: {
        /**
         * @description 获取表格数据
         */
        getTableData (page) {
            this.$post('/student/student_user_invite_award%5Cget_list', {
                page_id: page || this.page.now_page,
                keyword: this.formData.keyword,
                sort_field: this.filterData.sort_field,
                sort_type: this.filterData.sort_type,
                in_white_list: this.formData.in_white_list,
                invite_register_start_time: this.formData.invite_register_start_time,
                invite_register_end_time: this.formData.invite_register_end_time,
            }, resp => {
                if (resp.code == 1) {
                    this.tableData = resp.data.list;
                    this.page.now_page = resp.data.page.now_page;
                    this.page.total_count = resp.data.page.total_count;
                    this.page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 获取邀请人数据
         */
        getDialogData (page) {
            this.$post('/student/student_user_invite_award%5Cget_invite_new_user_list', {
                student_no: this.student_no,
                page_id: page || this.dialog_page.now_page,
                keyword: this.keyword_student_id,
                create_start_time: this.create_start_time,
                create_end_time: this.create_end_time,
            }, resp => {
                if (resp.code == 1) {
                    this.dialogData = resp.data.list;
                    this.dialog_page.now_page = resp.data.page.now_page;
                    this.dialog_page.total_count = resp.data.page.total_count;
                    this.dialog_page.page_size = resp.data.page.page_size;
                }
            });
        },

        /**
         * @description 查看详情
         */
        handleChackDetail (row) {
            this.student_no = row.student_no;
            this.getDialogData(1);
            this.dialog = !this.dialog;
        },

        /**
         * @description 数据排序
         */
        handleSortChanged(data) {
            console.log(data);
            let sortFieldMap = {
                register_student_count: 1,
                agent_level: 2,
                pay_order_complete_student_count: 7,
                pay_order_student_count: 4,
                award_money: 5,
                cannot_withdraw_award_money: 6,
                invite_complete_student_count: 3,
                pay_order_complete_award_money: 8,
                award_money_total: 9
            };

            let sortTypeMap = {
                ascending: 0,
                descending: 1
            };

            if (data.order) {
                this.filterData.sort_field = sortFieldMap[data.prop];
                this.filterData.sort_type = sortTypeMap[data.order];
            } else {
                this.filterData.sort_field = '';
                this.filterData.sort_type = '';
            }

            this.getTableData();
        },

        /**
         * @description 查看图片
         * @param   {String}    images    单图预览
         * @param   {Array}     images     多图预览
         */
        fnShowPreview(images) {
            if(images && Object.prototype.toString.call(images) == '[object String]') {
                this.$refs.preview.show([
                    images
                ]);
            } else if (images && Object.prototype.toString.call(images) == '[object Array]') {
                this.$refs.preview.show(images);
            } else {
                this.$notify({
                    title: '提示',
                    message: '图片类型错误',
                    type: 'warning',
                    duration: 2000
                });
            }
        },

        /**
         * @description 过滤函数
         */
        emptyFormatter(row, column, val) {
            if (!val) return '--';
            return val;
        },

        /**
         * 加入或移除白名单
         */
        handleWhiteList(row) {

            this.$confirm(`确认${row.in_white_list === 1 ? '移除' : '加入'}白名单吗?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                let params = {
                    student_no: row.student_no,
                    in_white_list: row.in_white_list === 1 ? 0 : 1
                };

                this.$post('/student/student_user_invite_award%5Cset_in_white_list', params, resp => {
                    if (resp.code === 1) {
                        this.$message.success('操作成功');
                        this.getTableData();
                    } else {
                        this.$notify({
                            title: '提示',
                            message: resp.msg,
                            duration: 2000,
                            type: 'warning'
                        });
                    }
                });
            });
        }
    }
};
</script>

<style lang="less" scoped>
    .container {
        background-color: #fff;
        padding: 10px 15px 5px 15px;
        display: flex;
        flex-direction: column;
        position: relative;
        width: 100%;
        .header-nav {
            position: relative;
            display: flex;
            flex-wrap: wrap;
            align-items: center;
            justify-content: space-between;
            .header-left, .header-right {
                display: flex;
                align-items: center;
                flex-wrap: wrap;
            }
            .header-new-box {
                display: flex;
                margin-right: 15px;
                align-items: center;
                color: #606266;
                margin-bottom: 10px;
                white-space: nowrap;
                &-title {
                    margin-right: 12px;
                    white-space: nowrap;
                }
            }
        }
        /* 表单 */
        .scope-price {
            width: 50px;
            height: 50px;
        }
        /* 字体高亮 */
        .cursor-font {
            color: rgb(64, 158, 255);
            cursor: pointer;
        }
        /* 分页 */
        .pagination-wrapper {
            padding-top: 10px;
            padding-bottom: 10px;
            .el-pagination {
                float: right;
                padding: 0;
                background-color: transparent;
            }
        }
    }
    /* 排序 */
    .sort-wrap {
        display: flex;
        align-items: center;
        justify-content: center;
        cursor: pointer;
        .sort-spliter {
            width: 1px;
            height: 12px;
            background: rgba(235, 235, 235, 1);
            margin: 0 10px;
            }
            .btn-sort {
            font-size: 14px;
            line-height: 1;
            color: #ccc;
            &:hover {
                color: #20a0ff;
            }
        }
    }
    .el-button-left {
        margin-left: 40px;
    }
    .el-button-right {
        margin-right: 10px;
    }
</style>
<style lang="less">
    .politeActiveDetail{
        .el-table__body-wrapper .el-table__body .el-table__row td {
            border-right: none;
        }
        .el-table__header-wrapper .el-table__header .has-gutter tr th {
            border-right: none;
        }
        .el-table .el-table_1_column_6  .cell {
            position: relative;
        }
        .el-range-editor--small .el-range-separator{
            padding: 0 1px !important;
        }
        .el-dialog__header {
            padding-top: 10px;
            box-shadow:0px 1px 0px 0px rgba(233,233,233,1);
            display: flex;
            align-items: center;
            background: #FAFAFA;
            border-radius: 4px 4px 0 0;
            .el-dialog__title {
                font-weight: 500;
                color: #333;
                font-size: 14px;
            }
            .el-icon-close:before {
                color: rgb(194,194,194);
                font-size: 17px;
                font-weight: bold;
            }
            .el-dialog__headerbtn {
                top: 14px;
            }
        }
        .dialogPolite .el-table__body-wrapper {
            height: auto !important;
            max-height: 400px !important;
        }
    }
</style>
